package com.fms.handler;

import com.fms.exception.CheckException;
import com.fms.result.ResultData;
import com.fms.exception.NotLoginException;
import com.fms.exception.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(AccessDeniedException.class)
    public ResultData handleAccessDeniedException(ServiceException e){
        log.info(e.getMessage());
        e.printStackTrace();
        return ResultData.error(e.getMessage()+",你无权访问");
    }
    @ExceptionHandler(ServiceException.class)
    public ResultData handleServiceException(ServiceException e){
        log.info(e.getMessage());
        e.printStackTrace();
        return ResultData.error(e.getMessage());
    }

    @ExceptionHandler(NotLoginException.class)
    public ResultData handleNotLoginException(NotLoginException e){
        log.info(e.getMessage());
        e.printStackTrace();
        return ResultData.error(e.getMessage()).setCode(e.getCode());
    }

    @ExceptionHandler(CheckException.class)
    public ResultData handleCheckException(CheckException e){
        log.info(e.getMessage());
        e.printStackTrace();
        return ResultData.error(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResultData handleException(Exception e){
        log.info(e.getMessage());
        e.printStackTrace();
        return ResultData.error("System exception, Please Wait...");
    }
}
